﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

public class SavedGameManager
{
    string saveGameFolder = "\\SavedGames\\";

    public void SaveGame(Player player, Day maxDay)
    {
        if (!Directory.Exists(Directory.GetCurrentDirectory() + saveGameFolder))
            Directory.CreateDirectory(Directory.GetCurrentDirectory() + saveGameFolder);

        StreamWriter writer = new StreamWriter(Directory.GetCurrentDirectory() + saveGameFolder + player.Name + ".txt");

        writer.WriteLine(player.Carisma);
        writer.WriteLine(player.Inteligencia);
        writer.WriteLine(player.Percepcao);
        writer.WriteLine((int) maxDay);

        writer.Close();
    }

    public bool CheckSavedGame()
    {
        string[] files;
        try
        {
            files = Directory.GetFiles(Directory.GetCurrentDirectory() + saveGameFolder, "*.txt");
        }
        catch (Exception e)
        {
            return false;
        }

        if (files.Length > 0)
            return true;
        return false;
    }


    internal List<SavedGame> GetSavedGames()
    {
        List<SavedGame> saveList = new List<SavedGame>();
        string[] files;

        try
        {
            files = Directory.GetFiles(Directory.GetCurrentDirectory() + saveGameFolder, "*.txt");


            foreach (string file in files)
            {
                try
                {
                    SavedGame save = new SavedGame();

                    StreamReader reader = new StreamReader(file);

                    string[] aux = file.Split('\\');
                    string name = aux[aux.Length - 1];
                    name = name.Replace(".txt", "");

                    save.Name = name;
                    save.Charisma = Int32.Parse(reader.ReadLine());
                    save.Intelligence = Int32.Parse(reader.ReadLine());
                    save.Perception = Int32.Parse(reader.ReadLine());
                    save.MaxDay = (Day) Int32.Parse(reader.ReadLine());

                    reader.Close();

                    saveList.Add(save);
                }
                catch (Exception e)
                { 
                }
                

            }
        }
        catch (Exception e)
        {
        }

        return saveList;
    }
}
